//
//  HomeTableViewController.swift
//  weibo
//
//  Created by 陈经纬 on 16/7/18.
//  Copyright © 2016年 陈经纬. All rights reserved.
//

import UIKit
import SVProgressHUD
private let bt = titleButton()


class HomeTableViewController: BaseTableViewController {
    
    /// 微博数据
    var statuses :[StatusViewModel]?{
        didSet{
            tableView.reloadData()
        }
    }


    override func viewDidLoad() {
        
        
        if isLogin == false {
            visitView?.setupVisitData("关注一些人.你会很高兴", imageName: nil)
            return
        }
        
        //添加导航条上的左右按钮
//        navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "navigationbar_friendattention"), style: UIBarButtonItemStyle.Plain, target: self, action: #selector(HomeTableViewController.leftBarButtonClick))
//        navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "navigationbar_pop"), style: UIBarButtonItemStyle.Plain, target: self, action: #selector(HomeTableViewController.rightBarButtonClick))
        navigationItem.leftBarButtonItem = UIBarButtonItem(image: "navigationbar_friendattention",  target: self , action:#selector(HomeTableViewController.leftBarButtonClick) )
        navigationItem.rightBarButtonItem = UIBarButtonItem(image: "navigationbar_pop",  target: self , action:#selector(HomeTableViewController.rightBarButtonClick) )
        
        //自定义导航条上的标题按钮
//        let titleBtn = titleButton()
        let title = UserAccount.loadUserAccount()?.screen_name
        
        bt.setTitle(title, forState: UIControlState.Normal)
        bt.addTarget(self, action: #selector(HomeTableViewController.titleBtnCilck), forControlEvents: UIControlEvents.TouchUpInside)

        navigationItem.titleView = bt
        
        
        //注册通知,解决弹出菜单点击其他位置,虽然弹出的菜单能收回,但是标题按钮的图片不会回去,只有点击标题按钮才回去这个BUG
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(HomeTableViewController.titleBtnChange), name: CJWNoticationTitleImageUp, object: nil)
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(HomeTableViewController.titleBtnChange), name: CJWNoticationTitleImageDown, object: nil)
        
        /**
         *  显示微博
         */
        
        loadData()
        
        JWLog(self.statuses)
        
    }
    deinit
    {
        // 移除通知
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    //MARK:内部控制方法
    
    //显示微博
    private func loadData()
    {
        NetWorkingTool.shareInstance.loadStaues { (array, error) -> () in
            // 1.安全校验
            if error != nil
            {
                SVProgressHUD.showErrorWithStatus("获取微博数据失败", maskType: SVProgressHUDMaskType.Black)
                return
            }
            guard let arr = array else
            {
                return
            }
            
            // 2.将字典数组转换为模型数组
            var models = [StatusViewModel]()
            for dict in arr
            {
                let status = Status(dict: dict)
                //用StatusViewModel吧Status包装起来
                let ViewModel = StatusViewModel(status:status)
                models.append(ViewModel)
            }
            
            // 3.保存微博数据
            self.statuses = models
            JWLog(self.statuses)
            
        }
    }
    
    //执行通知方法
    @objc private func titleBtnChange(){
        bt.selected = !bt.selected
    }
    
    
    
    /**
     点击左按钮事件 private代表私有
     */
    @objc private func leftBarButtonClick(){
        JWLog("")
    }
    /**
     点击右按钮事件
     */
    @objc private func rightBarButtonClick() {
        let qrVC = UIStoryboard(name: "QRCodeViewController", bundle: nil)
        
        let qrPresent = qrVC.instantiateInitialViewController()
        presentViewController(qrPresent!, animated: true, completion: nil)
    }
    
    /**
     点击标题事件
     */
    @objc private func titleBtnCilck(){
//        bt.selected = !bt.selected
        
//        //modal出小的列表控制器
//        let pop = UIStoryboard(name: "popPresentationControl", bundle: nil)
//        guard let menuView = pop.instantiateInitialViewController() else
//        {
//            return
//        }
//        
//        //自定义转场动画
//        //设置转场代理
//        menuView.transitioningDelegate = self
//        
//        menuView.modalPresentationStyle = UIModalPresentationStyle.Custom
        let pop = popPresentationControl.popVC()
        
        
        
        //自定义转场动画
        //设置转场代理
        //代码优化:把self用新建的类包装起来,把所有的pop代理方法在新建类实现,需要懒加载新建的类
        pop.transitioningDelegate = popManager

        pop.modalPresentationStyle = UIModalPresentationStyle.Custom

        presentViewController(pop, animated: true, completion: nil)
        
    }
    
    //代码优化:把self用新建的类包装起来,把所有的pop代理方法在新建类实现,需要懒加载新建的类
    lazy var popManager: CJWPoprensionManger = {
        let popManger = CJWPoprensionManger()
        
        popManger.presentFrame = CGRect(x: 100, y: 45, width: 200, height: 300)
        return popManger
        
        
    }()
    
    
    }


extension HomeTableViewController {
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.statuses?.count ?? 0
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        // 1.取出cell
        let cell = tableView.dequeueReusableCellWithIdentifier("homeCell", forIndexPath: indexPath) as! HomeTableViewCell
        // 2.设置数据
        cell.status = statuses![indexPath.row]
        // 3.返回cell
        return cell
    }
}

//extension HomeTableViewController:UIViewControllerTransitioningDelegate{
//    }
//extension HomeTableViewController: UIViewControllerAnimatedTransitioning
//{
//}